<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model;

use think\Model;
use think\Validate;
use think\Config;

class AdminModel extends  Model
{
    protected $table = 'yui_admin';
    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model($scene){
        $rule = [
            'username' => 'unique:admin|require|alphaNum|token',
            'password' => 'require|alphaNum',
            'name' => 'require|chs',
            'gender' => '\d+',
            'email' => '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$',
            'tel' => '^1[34578]{1}\d{9}$',
            'qq' => '\d+',
            'wchat' => 'alphaNum'
        ];
        $msg = [
            'username.require' => '用户名不能为空!',
            'username.alphaNum' => '用户名必须为字母或数字!',
            'password.require' => '密码不能为空!',
            'password.alphaNum' => '密码必须为字母或数字!',
            'name.require' => '姓名不能为空!',
            'name.chs' => '姓名只能为中文!',
            'gender' => '性别输入不合法!',
            'email' => '邮件格式不合法!',
            'tel' => '电话号码不合法!',
            'qq' => 'QQ格式不合法!',
            'wchat.alphaNum' => '微信格式不合法!'
        ];
        if ($scene=="edit"){
            $rule['username']='require|alphaNum';
        }else if ($scene=="add"){
            $rule['username']='unique:admin|require|alphaNum';
            $msg['username.unique']='用户名已经存在!';
        }
        $validate = new Validate($rule, $msg);
        $result = $validate->check(input('post.'));
        if(!$result){
            return [
                'code'=>0,
                'text'=> $validate->getError()
            ];
        }
        return ['code'=>1];
    }

    /*
    * 根据ID获取管理员信息
    * */
    public function getById($id){
        return $this->where(["id"=>$id])->find();
    }

    /*
    * 根据ID获取管理员信息
    * */
    public function getByRoleId($role_id){
        return $this->where(["role_id"=>$role_id])->select();
    }

    /*
    * 根据MAP获取管理员信息
    * */
    public function getByMaps($map){
        return $this->where($map)->find();
    }

    /*
     * 管理员列表
     * */
   public function listAll(){
       $fields='id,username,name,gender,email,tel,qq,wchat,description,role_id,status,created_at,updated_at';
       return $this->field($fields)->order('updated_at desc')->select();
   }

    /*
     * 新增管理员
     * */
    public function add(){
        $data=input('post.');
        $data['password']=md5($data['password']);
        $data['created_at']=time();
        $data['updated_at']=time();
        unset($data['__token__']);
        if ($data['role_id']!=Config::get('superRoleId')){
            if($this->save($data)){
                $params=[
                    Config::get('op_type')[0],
                    Config::get('op_module')[0],
                    Config::get('op_text')[0],
                    $data['username']
                ];
                tag('logs_db',$params);
                return op_success();
            }else{
                return op_fail();
            }
        }
        return op_superadmin();
    }

    /*
     * 修改管理员
     * */
    public function edit(){
        $data=input('post.');
        if (strlen($data['password'])!=32)
            $data['password']=md5($data['password']);
        $data['updated_at']=time();
        unset($data['__token__']);
        $id=input('post.id/d');
        $super=$this->getByRoleId(Config::get('superRoleId'));
        $super_ids=[];
        foreach ($super as $u){
            array_push($super_ids,$u->id);
        }
        if (in_array($id,$super_ids)){
            unset($data['role_id']);
            if($this->where('id',$id)->update($data)){
                $params=[
                    Config::get('op_type')[1],
                    Config::get('op_module')[0],
                    Config::get('op_text')[0],
                    $data['username']
                ];
                tag('logs_db',$params);
                return op_success();
            }
            return op_fail();
        }
        if ($data['role_id']!=Config::get('superRoleId')){
            if($this->where('id',$id)->update($data)){
                $params=[
                    Config::get('op_type')[1],
                    Config::get('op_module')[0],
                    Config::get('op_text')[0],
                    $data['username']
                ];
                tag('logs_db',$params);
                return op_success();
            }
        }
        return op_fail();
    }

    /*
     * 删除管理员
     * */
    public function del(){
        $super=$this->getByRoleId(Config::get('superRoleId'));
        $super_ids=[];
        foreach ($super as $u){
            array_push($super_ids,$u->id);
        }
        $ids=input('post.ids/a');
        if(!count(array_intersect($super_ids,$ids))){
            $op_name=[];
            foreach ($ids as $id){
                array_push($op_name,$this->getById($id)->toArray()['username']);
            }
            if ($this->destroy($ids)){
                $params=[
                    Config::get('op_type')[2],
                    Config::get('op_module')[0],
                    Config::get('op_text')[0],
                    implode(",", $op_name)
                ];
                tag('logs_db',$params);
                return op_success();
            }
            return op_fail();
        }
        return op_superadmin();
    }

}